<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

class ModuleMysqli
{
    private $host;
    private $user;
    private $password;
    private $charset;
    private $database;
	private $link;

    /**
     * 新建数据库连接对象，测试数据库连接
     *
     * @param string $host            
     * @param string $user            
     * @param string $password            
     * @param string $charset            
     * @param string $database            
     */
    function __construct($host, $user, $password, $charset = 'utf8', $database)
    {
        $this->host = $host;
        $this->user = $user;
        $this->password = $password;
        $this->charset = $charset;
        $this->database = $database;
		$this->connect();
    }

    /**
     * 连接数据库
     *
     * @param string $host            
     * @param string $user            
     * @param string $password            
     * @param string $charset            
     * @param string $database            
     * @return object 连接标识符
     */
    private function connect()
    {
        $this->link = mysqli_connect($this->host, $this->user, $this->password) or die('数据库连接失败<br />ERROR ' . mysqli_connect_errno() . ':' . mysqli_connect_error());
        mysqli_set_charset($this->link, $this->charset) or die('charset设置错误，请输入正确的字符集名称<br />ERROR ' . mysqli_errno($this->link) . ':' . mysqli_error($this->link));
        mysqli_select_db($this->link, $this->database) or die('未找到数据库，请输入正确的数据库名称<br />ERROR ' . mysqli_errno($this->link) . ':' . mysqli_error($this->link));
    }

	/*数据库执行语句，可执行查询添加修改删除等任何sql语句*/
	public function query($sql)
	{
		$this->result = mysqli_query($this->link, $sql) or die('SQL语句执行失败，请检查！<br />ERROR ' . mysqli_errno($this->link) . ':' . mysqli_error($this->link));
		return $this->result;
	}    

    /**
     * 查询指定记录
     *
     * @param string $query            
     * @param string $result_type            
     * @return array|boolean
     */
    public function getRow($query, $result_type = MYSQLI_ASSOC)
    {
        $result=$this->query($query);
        if ($result && mysqli_num_rows($result) > 0) {
            $row = mysqli_fetch_array($result, $result_type);
        } else {
            $row = false;
        }
        return $row;
    }

    /**
     * 查询所有记录
     *
     * @param string $query            
     * @param string $result_type            
     * @return array|boolean
     */
    public function getAll($query, $result_type = MYSQLI_ASSOC)
    {
        $result=$this->query($query);
        if ($result && mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_array($result, $result_type)) {
                $rows[] = $row;
            }
        } else {
            $rows = false;
        }
        return $rows;
    }
	
	//获得最后一条记录id
    public function getInsertid()
	{
        return mysqli_insert_id($this->link);
    }
	
	// 获取操作影响行数
	public function getTotal()
	{
		return mysqli_affected_rows($this->link);
	}
	
	//释放结果集
	public function free()
	{
		@mysqli_free_result($this->result);
	}
	
	//析构函数，自动关闭数据库,垃圾回收机制
	public function __destruct()
	{
		if (!empty ($this->result)) {
			$this->free();
		}
		mysqli_close($this->link);
	}
}